package com.sunchangming.stmdb.xgen;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

@XStreamAlias("variable")
public class Variable {
	@XStreamAsAttribute
	String name;
	@XStreamAsAttribute
	String type;
	@XStreamAsAttribute
	String key;
	@XStreamAsAttribute
	String value;

	@XStreamAsAttribute
	String from;

	@XStreamAsAttribute
	boolean marshalAsString;

	public String getFrom() {
		return (from != null && !from.isEmpty()) ? from : name;
	}

	Bean parent;

	/** 支持这个属性的类型： boolean、byte、double、float、int、short、long 、string */
	@XStreamAsAttribute
	String defaultvalue;

	Variable(String name, Type t) {
		this.name = name;
		this.mainType = t;
	}

	@XStreamOmitField
	private Type mainType;
	@XStreamOmitField
	private Type keyType;
	@XStreamOmitField
	private Type valueType;

	void compile() {
		if (this.name == null || this.name.isEmpty())
			throw new ConfigError(this.parent.getFullName() + "中有个变量没有名字，请补上");
		if (!Main.isValidForName(this.name))
			throw new ConfigError(this.parent.getFullName() + "中变量名" + name + "不合法");

		if (this.type == null || this.type.isEmpty())
			throw new ConfigError(this.parent.getFullName() + "中变量" + name + "必须得具有一个类型");

		this.mainType = Types.getInstance().get(type);
		if (this.mainType == null)
			this.mainType = Types.getInstance().get(parent.parent.getFullName() + "." + type);

		if (this.mainType == null)
			throw new ConfigError(this.parent.getFullName() + "中变量" + name + "的类型'" + type + "'无法被解析");

		if (key != null && !key.isEmpty() && (this.keyType = Types.getInstance().get(key)) == null)
			throw new ConfigError(this.parent.getFullName() + "中变量" + name + "的类型'" + key + "'无法被解析");

		if (value != null && !value.isEmpty()) {

			this.valueType = Types.getInstance().get(value);
			if (this.valueType == null)
				this.valueType = Types.getInstance().get(parent.parent.getFullName() + "." + value);

			if (this.valueType == null)
				throw new ConfigError(this.parent.getFullName() + "中变量" + name + "的类型'" + value + "'无法被解析");
		}

	}

	public String getName() {
		return name;
	}

	public String getTypeDeclStr() {
		return mainType.getName(this);
	}

	public String getDefaultValue() {
		return mainType.getDefaultValue(this);
	}

	public Type getMainType() {
		return mainType;
	}

	public Type getKeyType() {
		return keyType;
	}

	public Type getValueType() {
		return valueType;
	}

	public String getMarshalStr() {
		return mainType.getMarshalStr(this, "dis");
	}

	public String getUnmarshalStr() {
		return mainType.getUnmarshalStr(this, "dis", true);
	}

	public String getLocalUnmarshalStr() {
		return mainType.getUnmarshalStr(this, "dis", false);
	}

	public String getTestEqualsStr() {
		return mainType.getTestEqualsStr(this, "_o1_");
	}

	public String getHashCodeStr() {
		return mainType.getHashCodeStr(this);
	}
}
